package com.linlang.grasharepro.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.adapter.ChatAdapter;
import com.linlang.grasharepro.dao.NewMessageDB;
import com.linlang.grasharepro.model.NewMessageDomain;
import com.linlang.grasharepro.model.ShareCoenten;
import com.linlang.grasharepro.utils.ListViewUitls;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by lixin on 2016-8-8.
 */
public class MessageActivity extends Activity {

    private ListView msgListView;
    private ImageView iv_sendMessage,iv_back;
    private EditText et_msg;
    private TextView tv_userName;
    private ArrayList<NewMessageDomain> share_list = new ArrayList<NewMessageDomain>();
    private NewMessageDB newMessageDB;
    private ShareCoenten info = null;
    ChatAdapter ad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        // 获取从列表当中传递过来的数据
        Intent intent = getIntent();
        Bundle bund = intent.getExtras();
        info = (ShareCoenten) bund.getSerializable("shareContent");

        //后退按钮
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageActivity.this.finish();
            }
        });

        initView();
    }

    private void initView() {
        tv_userName = (TextView) findViewById(R.id.tv_userName);
        tv_userName.setText(info.getUserName());
        et_msg = (EditText) findViewById(R.id.et_msg);

        //获取用户登陆信息
        SharedPreferences preferences = getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");

        newMessageDB = new NewMessageDB(this);
        newMessageDB.open();

        share_list = newMessageDB.getMyprivateLetter(account, info.getUserName());

        msgListView = (ListView) findViewById(R.id.message_list);
        ad = new ChatAdapter(share_list, this);
        msgListView.setAdapter(ad);
        ListViewUitls.setListViewHeightBasedOnChildren(msgListView);


        //发送按钮
        iv_sendMessage = (ImageView) findViewById(R.id.iv_sendMessage);
        iv_sendMessage.setOnClickListener(new MyOnClickListener());
    }

    /**
     * 控件的监听事件
     */
    private class MyOnClickListener implements View.OnClickListener {

        private int index = 0;
        public void onClick(View v) {
            leaveMessage();

            //获取用户登陆信息
            SharedPreferences preferences = getSharedPreferences("userInfo",
                    Activity.MODE_PRIVATE);
            String account = preferences.getString("account", "");
            share_list.clear();
            share_list.addAll(newMessageDB.getMyprivateLetter(account, info.getUserName()));
            ad.setList(share_list);
            ad.notifyDataSetChanged();
        }
    }

    /**
     * 私信
     */
    public void leaveMessage() {
        //获取用户登陆信息
        SharedPreferences preferences = getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");

        String dateStr = "";
        try {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            dateStr = simpleDateFormat.format(date);
        } catch (Exception e) {

        }

        //

        //新增新消息表
        NewMessageDomain newMessageDomain = new NewMessageDomain();
        newMessageDomain.setType("1");
        newMessageDomain.setTime(dateStr);
        newMessageDomain.setFromUser(account);
        newMessageDomain.setToUser(info.getUserName());
        newMessageDomain.setContent(et_msg.getText().toString());

        newMessageDB = new NewMessageDB(this);
        newMessageDB.open();
        newMessageDB.insertNewMessage(newMessageDomain);
        et_msg.setText("");
    }
}
